<?php
class Language_model extends MY_Model
{
	public
		$table = 'languages',
		$prKey = 'language_ID',
		$publishedField = 'language_published'
	;
	
	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'language_ID' => T_('Language'). ' (abbr)',
			'language_name' => T_('Language'),
			'language_published' => T_('Activity'),
			'language_modified' => T_('Last Add / Edit')
		);

		$this->rules = array(
			'language_name' => "trim|strip_tags|required|max_length[32]",
			'language_ID' => "trim|required|max_length[2]"
		);
	}
	
	function getSelectOptions($first_option = 'Select..')
	{
		$query = $this->db->get($this->table);
		$options = array('0' => $first_option);
		
		foreach ($query->result() as $row)
		{
			$options[$row->language_ID] = $row->language_name; 
		}
		
		return $options;
	}

	function getList()
	{
		$query = $this->db->get($this->table);
		
		foreach ($query->result() as $row)
		{
			$options[] = $row->language_ID; 
		}
		
		return $options;
	}
	
	function search($config = array())
	{
		$query = $this->db->get($this->language_model->table);

		$config = array_merge(array('rowsOnPage' => 10, 'order' => 'language_ID'), $config);
		if ($this->input->post('per_page'))
		{
			$config['rowsOnPage'] = $this->input->post('per_page');
			$per_page = 0;
		}		
		else if ($this->input->get('per_page'))
		{
			$per_page = $this->input->get('per_page');
		}
		else
		{
			$per_page = 0;
		}

		$order = $this->input->get_post('order');
		$direction = $this->input->get_post('direction');
		$search = $this->input->get_post('search');
		// main sql		
		$sql = '';
		$sql .= ' FROM languages c';
		$sql .= ' WHERE 1 = 1';  
			
		if ($search)
		{
			$sql .= ' AND c.language_name LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER by c.language_created'; 
				break;
			
			default:
				$sql .= ' ORDER by c.language_ID';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		// pagination config
		$config['base_url'] = Xurl_add(Xregenerate_url('per_page,search', true), 'search', $search);
		$config['per_page'] = $config['rowsOnPage'];
		$config['total_rows'] = $this->getRowCount($sql);

		$this->pagination->initialize($config); 
		
		// main sql
		$sql = 'SELECT *' . $sql . sprintf(' LIMIT %d, %d', $per_page, $config['per_page']); 

		$this->data['query'] = $this->db->query($sql);
		//echo $per_page;
		$this->data['per_page'] = $per_page;
		$this->data['search'] = $search; 
	}	
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$error = "";
		$report = "";
		foreach ($items as $id)
		{	
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			$report .= ",1"; // deleted 	
			
			if (is_null($res))
			{
				$error = true;
			}				
		}
		return array("error" => $error, "report" => substr($report, 1));
	}
		
}
?>